package cn.edu.csust.coursecard.utils;

import cn.edu.csust.coursecard.common.CodeEnum;
import cn.edu.csust.coursecard.exception.BaseException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
@Data
@ConfigurationProperties(prefix = "jwt")
public class TokenUtils {
    
    private String secret;
    
    private Long expiration;
    
    private String tokenName;
    
    private Long refresh_expiration;

    /**
     * 生成令牌
     */
    public String generateToken(Integer userId,Boolean isAdmin) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("sub", userId);
        claims.put("isAdmin",isAdmin);
        claims.put("create", new Date());
        return generateToken(claims);
    }

    /**
     * 从claims生成令牌
     */
    private String generateToken(Map<String, Object> claims) {
        Date expirationDate = new Date(System.currentTimeMillis() + expiration);
        return Jwts.builder().setClaims(claims)
                .setExpiration(expirationDate)//设置过期时间
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
    
    /**
     * @return 生成RefreshToken
     */
    public String generateRefreshToken(Integer doctorId) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("sub", doctorId + "");
        claims.put("create", new Date());
        //设置过期时间为七天；
        Date expirationDate = new Date(System.currentTimeMillis() + refresh_expiration);
        return Jwts.builder().setClaims(claims)
                .setExpiration(expirationDate)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
    
    /**
     * 从令牌中获取userId
     *
     * @param token 令牌
     */
    public Integer getIdFromToken(String token) {
        Integer id;
        try {
            Claims claims = getClaimsFromToken(token);
            id = Integer.parseInt(claims.getSubject());
        } catch (ExpiredJwtException e) {
            id = null;
            throw new BaseException(CodeEnum.REQUEST_FAILED.getCode(), "验证失败，请重新登录！");
        }
        return id;
    }

    /**
     * 从令牌中获取userId
     *
     * @param token 令牌
     */
    public Boolean getIsAdminFromToken(String token) {
        Boolean isAdmin;
        try {
            Claims claims = getClaimsFromToken(token);
            isAdmin = (Boolean) claims.get("isAdmin");
        } catch (ExpiredJwtException e) {
            isAdmin = null;
            throw new BaseException(CodeEnum.REQUEST_FAILED.getCode(), "验证失败，请重新登录！");
        }
        return isAdmin;
    }
    
    
    /**
     * 从令牌中获取数据声明，
     *
     * @param token 令牌
     * @return 数据声明
     */
    private Claims getClaimsFromToken(String token) {
        return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
    }
    
    /**
     * 判断令牌是否过期
     *
     * @param token 令牌
     * @return 是否过期
     */
    public Boolean isTokenExpired(String token) {
        try {
            Claims claims = getClaimsFromToken(token);
            Date expiration = claims.getExpiration();
            return expiration.before(new Date());
        } catch (Exception e) {
            return true;
        }
    }
    
    /**
     * 验证令牌
     *
     * @param token 令牌
     * @return 是否有效
     */
    public Boolean validateToken(String token) {
        return !isTokenExpired(token);
    }
    
    /**
     * 刷新refreshToken
     *
     * @param refreshToken 刷新令牌
     */
    public String getNewRefreshToken(String refreshToken) {
        try {
            Claims claims = getClaimsFromToken(refreshToken);
            Date expiration = claims.getExpiration();
            //如果refreshToken距过期时间不足1小时，刷新refreshToken
            if (expiration.getTime() - System.currentTimeMillis() < 1000 * 60 * 60 * 12) {
                return generateRefreshToken(getIdFromToken(refreshToken));
            }
            return refreshToken;
        } catch (Exception e) {
            return refreshToken;
        }
    }
    
    
}

